package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.PsyKnowledgeMapper;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.PsyKnowledge;
import com.hualan.pojo.Result;
import com.hualan.service.PsyKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsyKnowledgeServiceImpl implements PsyKnowledgeService {
    @Autowired
    private PsyKnowledgeMapper psyKnowledgeMapper;
    @Override
    public PageResult queryAllPsyKnowledge(String title, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
       List<PsyKnowledge> psyKnowledgeList = psyKnowledgeMapper.queryAllPsyKnowledge(title);
       Page<PsyKnowledge> p = (Page<PsyKnowledge>) psyKnowledgeList;
       return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Result addPsyKnowledge(PsyKnowledge psyKnowledge) {
        try {
            psyKnowledgeMapper.addPsyKnowledge(psyKnowledge);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加心理知识失败");
        }
    }

    @Override
    public Result updatePsyKnowledge(PsyKnowledge psyKnowledge) {
       try {
           psyKnowledgeMapper.updatePsyKnowledge(psyKnowledge);
           return Result.success();
       }catch (Exception e){
           e.printStackTrace();
           return Result.error("更新心理知识失败");
       }
    }

    @Override
    public Result deletePsyKnowledge(List<Integer> ids) {
       try {
           psyKnowledgeMapper.deletePsyKnowledge(ids);
           return Result.success();
       }catch (Exception e){
           e.printStackTrace();
           return Result.error("删除心理知识失败");
       }
    }
}
